package com.zhuss.core.processor;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.core.resource.DeleteResourceBo;
import com.zhuss.bean.vo.core.resource.DetailResourceVo;
import com.zhuss.common.AbstractProcessor;
import com.zhuss.common.Processor;
import com.zhuss.core.db.dao.ext.ResourceDao;
import com.zhuss.core.db.entity.Resource;
import com.zhuss.db.entity.OpLog;
import com.zhuss.enums.IsEnum;
import com.zhuss.enums.LogActionEnum;
import com.zhuss.enums.MoudleEnum;
import com.zhuss.exception.AabException;
import com.zhuss.exception.ExceptionCode;
import com.zhuss.util.JsonUtil;

/**
 * 删除菜单资源 processor
 * @Author: zhushanshuo
 * @Date: 2019/9/24 19:54
 */
@Component("resourceDelete")
public class ResourceDelete extends AbstractProcessor<Object, DeleteResourceBo> implements Processor<Object, DeleteResourceBo> {

    @Autowired
    private ResourceDao resourceDao;
    
    @Autowired
	@Qualifier("resourceRead")
	private ResourceRead resourceRead;
    
    @Autowired
	@Qualifier("permissionResourceRead")
	private PermissionResourceRead permissionResourceRead;
    
    /**
     * 校验
     */
	@Override
	@SuppressWarnings("unchecked")
    public void check(DeleteResourceBo bo) throws AabException {
    	Long resourceId = bo.getResourceId();
    	List<Resource> sonList = resourceRead.listSon(resourceId);
    	if (!sonList.isEmpty()) {
    		throw new AabException(ExceptionCode.不能删除父级资源点);
    	}
    	
    	List<Long> permissionIdList = (List<Long>) permissionResourceRead.listPermissionId(resourceId);
    	if (!permissionIdList.isEmpty()) {
    		throw new AabException(ExceptionCode.不能删除已绑定权限的资源点);
    	}
    	
    	DetailResourceVo resource = (DetailResourceVo) resourceRead.detail(resourceId);
    	if (IsEnum.是.sameKey(resource.getIsPublic())) {
    		throw new AabException(ExceptionCode.不能删除公共资源);
    	}
    }
    
    /**
     * 操作方法
     */
    @Override
    public Object process(DeleteResourceBo param) throws AabException {
    	
    	Long resourceId = param.getResourceId();
    	
    	String name = ((DetailResourceVo) resourceRead.detail(resourceId)).getName();
    	
    	Resource record = new Resource();
    	record.setResourceId(resourceId);
    	record.setDeleted(param.getDeleted());
    	
    	record.setModifierId(param.getConcurrentUserId());
    	record.setModifier(param.getConcurrentUserName());
    	record.setModifyTime(new Date());
    	resourceDao.delete(record);
    	param.setName(name);
        return null;
    }
    
    /**
     * 操作日志
     */
	@Override
	public Object log(DeleteResourceBo param) throws AabException {
		OpLog record = new OpLog();
    	record.setMoudle(MoudleEnum.基础模块.key());
    	record.setAction(LogActionEnum.菜单资源删除.key());
    	record.setTargetId(param.getResourceId());
    	record.setTargetName(param.getName());
    	record.setContent("参数: " + JsonUtil.toJson(param));
    	record.setReason(param.getExceptionReason());
    	
    	record.setCreatorId(param.getConcurrentUserId());
    	record.setCreator(param.getConcurrentUserName());
    	record.setCreateTime(new Date());
    	log(record);
    	return null;
	}
}
